package com.mcy.springbatch.job;

import com.mcy.springbatch.job.BaseJobConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.FlowBuilder;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

/**
 * 利用Split进行并发执行step的demo
 */
@Slf4j
@Configuration
public class ESpliteJobConfig extends BaseJobConfig {

    /**
     * 利用split进行并发执行两个flow
     * @return
     */
    @Bean
    public Job splitDemoJob(){
        return jobBuilderFactory.get("splitDemoJob")
                .incrementer(new RunIdIncrementer())
                .start(splitDemoFlow1())
                .split(new SimpleAsyncTaskExecutor())
                .add(splitDemoFlow2())
                .end()
                .build();
    }

    @Bean
    public Flow splitDemoFlow1(){
        return new FlowBuilder<Flow>("splitFlowDemo1")
                .start(splitDemoStep1())
                .build();
    }

    @Bean
    public Flow splitDemoFlow2(){
        return new FlowBuilder<Flow>("splitFlowDemo2")
                .start(splitDemoStep2())
                .next(splitDemoStep3())
                .build();
    }

    @Bean
    public Step splitDemoStep1() {
        return stepBuilderFactory.get("splitDemoStep1")
                .tasklet((stepContribution, chunkContext) -> {
                    log.info("splitDemoStep1...");
                    return RepeatStatus.FINISHED;
                })
                .build();
    }
    @Bean
    public Step splitDemoStep2() {
        return stepBuilderFactory.get("splitDemoStep2")
                .tasklet((stepContribution, chunkContext) -> {
                    log.info("splitDemoStep2...");
                    return RepeatStatus.FINISHED;
                })
                .build();
    }
    @Bean
    public Step splitDemoStep3() {
        return stepBuilderFactory.get("splitDemoStep3")
                .tasklet((stepContribution, chunkContext) -> {
                    log.info("splitDemoStep3...");
                    return RepeatStatus.FINISHED;
                })
                .build();
    }




}
